package smedi.ismedi.web.bff.controller.report;

import com.smedi.ismedi.report.api.exception.SystemExcption;
import com.smedi.ismedi.report.api.feign.RemoteReportAcceptFeignApi;
import com.smedi.ismedi.report.api.model.ReportAccept;
import com.smedi.ismedi.report.api.model.vo.ReportAcceptExportVo;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


@Api(tags = {"资料管理-单位工程验收"})
@RequestMapping(value = "/webbff/v1/accept")
@RestController
public class RemoteReportAcceptController {

    @Autowired
    RemoteReportAcceptFeignApi remoteReportAcceptFeignApi;

    /**
     * 列表查询
     * @return
     */
    @ApiOperation(value = "单位工程验收列表查询", nickname = "单位工程验收列表查询")
    @PostMapping("/list")
    public ResponseData<PaginationData<ReportAccept>> getList(@RequestBody ReportAccept reportAccept){
        return remoteReportAcceptFeignApi.getList(reportAccept);
    }


    @ApiOperation(value = "详情", nickname = "详情")
    @GetMapping("/details/{id}")
    public ResponseData<ReportAccept> details(@PathVariable("id")  Long id){
        return remoteReportAcceptFeignApi.getDetails(id);
    }



    /**
     * 新增
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "单位工程验收新增", nickname = "单位工程验收新增")
    public ResponseData<String> add(@RequestBody ReportAccept reportAccept){
        return remoteReportAcceptFeignApi.add(reportAccept);
    }

    /**
     * 删除
     * @return
     */
    @GetMapping("/delete/{ids}")
    @ApiOperation(value = "单位工程验收删除", nickname = "单位工程验收删除")
    public ResponseData<Boolean> delete(@PathVariable("ids") Long[] acceptIds){
        return remoteReportAcceptFeignApi.delete(acceptIds);
    }


    @ApiOperation(value = "明细删除", nickname = "单位工程验收删除")
    @GetMapping("/delete/details/{id}")
    public ResponseData<String> deleteDetails(@PathVariable("id") Long id){
        remoteReportAcceptFeignApi.deleteDetails(id);

        return  new ResponseData<String>().success("删除成功");
    }

    /**
     * 修改
     * @return
     */
    @PostMapping
    @ApiOperation(value = "单位工程验收修改", nickname = "单位工程验收修改")
    public ResponseData<ReportAccept> edit(@RequestBody ReportAccept reportAccept){
        return remoteReportAcceptFeignApi.edit(reportAccept);
    }

    /**
     * 导出Excel
     * @return
     */
    @PostMapping("/exportExcel")
    @ApiOperation(value = "单位工程验收导出Excel", nickname = "单位工程验收导出Excel")
    public void exportExcel(@RequestBody ReportAcceptExportVo reportAccept, HttpServletResponse response){
        ResponseData<byte[]> responseData = remoteReportAcceptFeignApi.exportExcel(reportAccept);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(responseData.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出Word
     * @return
     */
    @PostMapping("/exportWord")
    @ApiOperation(value = "单位工程验收导出Word", nickname = "单位工程验收导出Word")
    public void exportWord(@RequestBody ReportAcceptExportVo reportAccept, HttpServletResponse response){
        ResponseData<byte[]> responseData = remoteReportAcceptFeignApi.exportWord(reportAccept);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(responseData.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出图片
     */
    @PostMapping("/export/signKey")
    @ApiOperation(value = "使用signKey下载文件", nickname = "使用signKey下载文件")
    public void exportBySignKey( HttpServletResponse response,  @RequestBody List<String> signKey){
        ResponseData<byte[]> responseData = remoteReportAcceptFeignApi.exportBySignKey(signKey);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("1.zip", "UTF-8"));
            outputStream.write(responseData.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @PostMapping("/test/signKey")
    @ApiOperation(value = "使用signKey下载文件", nickname = "使用signKey下载文件")
    public void test( HttpServletResponse response,  @RequestBody List<String> signKey){
        ResponseData<byte[]> responseData = remoteReportAcceptFeignApi.exportBySignKey(signKey);
        try {
            if(responseData.isSuccess()){
                ServletOutputStream outputStream = response.getOutputStream();
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("1.zip", "UTF-8"));
                response.setHeader("Content-Type", "application/octet-stream");
                response.setContentType("application/octet-stream; charset=UTF-8");
                outputStream.write(responseData.getData());
            }else {
                throw   new SystemExcption("文件下载异常");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
